﻿using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Markup;

namespace Gmantis.Controls.Chart
{
    internal class Themes
    {
        // Fields
        private static Dictionary<ChartTheme, ResourceDictionary> _dict = new Dictionary<ChartTheme, ResourceDictionary>();

        // Methods
        public static ResourceDictionary GetThemes(ChartTheme theme)
        {
            if ((theme == ChartTheme.None) || (theme == ChartTheme.Custom))
            {
                return null;
            }
            if (!_dict.ContainsKey(theme))
            {
                _dict.Add(theme, LoadThemeFromResources(theme));
            }
            return _dict[theme];
        }

        public static bool IsStandard(ResourceDictionary rd)
        {
            return ((rd != null) && _dict.ContainsValue(rd));
        }

        private static ResourceDictionary LoadThemeFromResources(ChartTheme theme)
        {
            ResourceDictionary dictionary = null;
            try
            {
                string name = "Silverlight.Chart.ThemesSL." + theme.ToString() + ".xaml";
                using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
                {
                    if (stream != null)
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            dictionary = XamlReader.Load(reader.ReadToEnd()) as ResourceDictionary;
                        }
                    }
                    return dictionary;
                }
            }
            catch
            {
            }
            return dictionary;
        }
    }
}
